package com.zrf.geekbang.spring.bean;

import com.zrf.geekbang.spring.bean.factory.DefaultUserFactory;
import com.zrf.geekbang.spring.bean.factory.UserFactory;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * @author zhouruifeng
 * @date 2020-07-05
 */
public class SingletonBeanRegistrationDemo {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        //注入配置类
        applicationContext.register(SingletonBeanRegistrationDemo.class);
        //注册外部单例对象
        UserFactory userFactory = new DefaultUserFactory();
        ConfigurableListableBeanFactory beanFactory = applicationContext.getBeanFactory();
        beanFactory.registerSingleton("userFactory", userFactory);

        //启动应用上下文
        applicationContext.refresh();

        UserFactory applicationContextBean = applicationContext.getBean("userFactory", UserFactory.class);
        System.out.println(applicationContextBean);
        System.out.println(applicationContextBean == userFactory);
    }
}
